package com.ywgt.controller;

import com.github.pagehelper.Page;
import com.ywgt.pojo.Comment;
import com.ywgt.service.CommentService;
import com.ywgt.util.ResponseUtil;
import io.swagger.annotations.ApiModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@CrossOrigin
@RestController
@RequestMapping("/admin/comment")
@ApiModel("评论管理")
public class CommentController {
    @Autowired
    CommentService commentService;

    /**
     *page: 1,
     *         limit: 20,
     *         userId: undefined,
     *         valueId: undefined,
     *         sort: 'add_time',
     *         order: 'desc'
     * @return
     */
    @GetMapping("/list")
    public Object getComments(String userId,String valueId,@RequestParam(required = false,defaultValue = "add_time") String sort,
                              @RequestParam(required = false,defaultValue = "desc") String order,
                              @RequestParam(required = false,defaultValue = "1") int page,
                              @RequestParam(required = false,defaultValue = "10") int limit){
        Page<Comment> comments=commentService.getComments(userId,valueId,page,limit,sort,order);
        List<Comment> items = comments.getResult().stream().filter(a->!a.getDeleted()).collect(Collectors.toList());
        long total = comments.getTotal();
        Map<String,Object> data=new HashMap<>();
        data.put("total",total);
        data.put("items",items);
        return ResponseUtil.ok(data);
    }

    @PostMapping("/delete")
    public Object delete(@RequestBody Comment comment){
        if(commentService.removeById(comment.getId())){
            return ResponseUtil.ok();
        }
        return ResponseUtil.fail();
    }


}
